import { MessageCode } from '../interface'
import { openWeb } from './hooks'
import { conbineUrlParams, getUrlInfo, getUrlParams } from '../utils/common'
export function useOnMsg({ code, params }: { code: MessageCode; params: any }) {
  if (!code) return

  messageMap[code](params)
}

export const messageMap = {
  // 服务授权
  [MessageCode.VALIDATE_AUTH](params: Index.ServerData) {
    if (params.url && ~params.url.indexOf('http')) {
      const dataParams = getUrlParams(params.url)
      const url = getUrlInfo(params.url)
      params.url = `${url.baseUrl}?${conbineUrlParams({
        ...dataParams.search,
        tfsmy_auth: params.authType || 0,
        tfsmy_real_name: params.sensitiveType || 0,
      })}${conbineHashOrNot(url, dataParams, params)}`
    }

    // console.log('params.url', params.url)

    openWeb(params)
  },

  [MessageCode.MODIFY_SHARE_MESSAGE](params: CustomShareAppMessage) {
    uni.$emit('modifyShareMessage', params)
  },
}

export function conbineHashOrNot(
  url: { hash?: string; baseUrl: string },
  dataParams: {
    hash: {
      [k: string]: string
    }
    search: {
      [k: string]: string
    }
  },
  params?: Index.ServerData
) {
  console.log('dataParams.hash', dataParams, url)
  if (url.hash) {
    return `${url.hash}?${conbineUrlParams({
      ...dataParams.hash,
      // tfsmy_auth: params.authType || 0,
      // tfsmy_real_name: params.sensitiveType || 0,
    })}`
  }

  return ''
}
